package cn.com.inossem.operation;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import cn.com.inossem.activity.ListJobsActivity;
import cn.com.inossem.config.WSConfig;
import cn.com.inossem.model.QueryProjectInfoBean;
import cn.com.inossem.model.QueryProjectListInfoBean;
import cn.com.inossem.network.NetworkConnect;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.Utils;

import com.foxykeep.datadroid.exception.ConnectionException;
import com.foxykeep.datadroid.exception.CustomRequestException;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.service.RequestService.Operation;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class ApplyCollectionOperation implements Operation {
	public static final String APPLY_OPERATION_TYPE = "apply_operation_type";
	public static final String APPLY_OPERATION_BEAN = "apply_operation_bean";

	public Bundle bundle;

	// 0收藏 1是申请2是取消收藏 3是取消申请
	@Override
	public Bundle execute(Context arg0, Request request)
			throws ConnectionException, DataException, CustomRequestException {

		// String applytype1 = request.getString(APPLY_OPERATION_TYPE);
		// int applytype = Integer.parseInt(applytype1);
		int applytype = Utils.type;
		System.out.println("applytype:" + applytype);
		Gson gson = new GsonBuilder().disableHtmlEscaping().create();
		List<QueryProjectInfoBean> projectForUser = new ArrayList<QueryProjectInfoBean>();
		String tojson = "";
		QueryProjectInfoBean bean;
		QueryProjectListInfoBean qb;
		List<String> listcollect = new ArrayList<String>();
		List<String> liststatus = new ArrayList<String>();
		String collect = "";
		String status = "";
		switch (applytype) {
		case 0:// 收藏
			bean = (QueryProjectInfoBean) request
					.getParcelable(APPLY_OPERATION_BEAN);
			collect = bean.collect;
			status = bean.status;
			bean.collect = "10994";
			bean.status = "";
			projectForUser.add(bean);
			qb = new QueryProjectListInfoBean();
			qb.setListProjectForUser(projectForUser);
			tojson = gson.toJson(qb).toString();
			bean.status = status;
			bean.collect = collect;
			break;
		case 1:
			bean = (QueryProjectInfoBean) request
					.getParcelable(APPLY_OPERATION_BEAN);
			collect = bean.collect;
			status = bean.status;
			bean.collect = "";
			bean.status = "10991";

			bean.salary = Utils.money;
			bean.remark = Utils.beizhu;

			System.out.println(bean.salary + "========" + bean.remark);
			projectForUser.add(bean);
			qb = new QueryProjectListInfoBean();
			qb.setListProjectForUser(projectForUser);
			tojson = gson.toJson(qb).toString();
			bean.status = status;
			bean.collect = collect;
			break;
		case 2:
			projectForUser = ListJobsActivity.Xuanzhongmessage;
			for (int i = 0; i < projectForUser.size(); i++) {
				listcollect.add(projectForUser.get(i).collect);
				liststatus.add(projectForUser.get(i).status);
				projectForUser.get(i).collect = "-1";
				projectForUser.get(i).status = "";
			}
			qb = new QueryProjectListInfoBean();
			qb.setListProjectForUser(projectForUser);
			tojson = gson.toJson(qb).toString();
			for (int i = 0; i < projectForUser.size(); i++) {
				listcollect.add(projectForUser.get(i).collect);
				liststatus.add(projectForUser.get(i).status);

				projectForUser.get(i).status = liststatus.get(i);
				projectForUser.get(i).collect = listcollect.get(i);
			}
			break;
		case 3:
			bean = (QueryProjectInfoBean) request
					.getParcelable(APPLY_OPERATION_BEAN);
			bean.collect = "";
			bean.status = "-1";
			projectForUser.add(bean);
			qb = new QueryProjectListInfoBean();
			qb.setListProjectForUser(projectForUser);
			tojson = gson.toJson(qb).toString();
			bean.status = status;
			bean.collect = collect;
			break;
		default:
			break;
		}

		System.out.println("QueryProjectInfoBean to json:" + tojson);

		NetworkConnect connection = new NetworkConnect(WSConfig.LOCAL_URL
				+ "projectInfo/projectForUser");
		connection.setPostText(tojson);
		String body = connection.execute();

		System.out.println("ApplyCollectionOperation:" + body);
		bundle = new Bundle();
		try {
			JSONObject jsonObject = new JSONObject(body);
			int errorFlag = jsonObject.getInt("errorFlag");

			if (errorFlag == WSConfig.RESULT_SUCESS) {
				// 设置登陆结果
				switch (applytype) {
				case 0:
					System.out.println("接口收藏成功");
					int collectNum = jsonObject.getInt("collectNum");
					Utils.messageInfo.userInfo.setCollectNum(collectNum + "");
					bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_APPLYSUCCESS,
							errorFlag);
					break;
				case 1:
					System.out.println("接口申请成功");
					int applyNum = jsonObject.getInt("applyNum");
					Utils.messageInfo.userInfo.setApplyNum(applyNum + "");
					bundle.putInt(
							WooRequestFactory.BUNDLE_EXTRA_POSITIONSUCCESS,
							errorFlag);
					break;
				case 2:// 取消收藏
					collectNum = jsonObject.getInt("collectNum");
					Utils.messageInfo.userInfo.setCollectNum(collectNum + "");
					bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_APPLYQUXIAO,
							errorFlag);
					break;
				case 3:// 取消申请
					System.out.println("取消申请成功");
					applyNum = jsonObject.getInt("applyNum");
					Utils.messageInfo.userInfo.setApplyNum(applyNum + "");
					bundle.putInt(
							WooRequestFactory.BUNDLE_EXTRA_POSITIONQUXIAO,
							errorFlag);
					break;

				default:
					break;
				}
			} else {
				switch (applytype) {
				case 0:
					bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_APPLYFAILURE,
							errorFlag);
					break;
				case 1:
					bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_APPLYFAILURE,
							errorFlag);
					break;
				case 2:
					bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_APPLYFAILURE,
							errorFlag);
					break;
				case 3:
					bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_APPLYFAILURE,
							errorFlag);
					break;

				default:
					break;
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return bundle;
	}
}
